// pages/taocan/taocan.js
const app = getApp(), core = require('../../utils/core'), util = require('../../utils/util');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isIos: false,
    summary:{},
    packageList:[],
    packageListMore:{page:1},
    packageIndex:0,
    packageOrderList:[],
    packageOrderListMore:{page:1},
    tabIdx: 1,
    showPop:false,
    payIdx: 1,
    ticketType:1,
    ticket:'',
    remarks: ''
  },
  tabClick(e) {
    this.setData({
      tabIdx: e.currentTarget.dataset.index
    })
  },
  togglePop(){
    this.setData({
      showPop: !this.data.showPop
    })
  },
  //开票类型
  ticketClick(e) {
    this.setData({
      ticketType: e.currentTarget.dataset.index
    })
  },
   //支付类型
  payClick(e) {
    this.setData({
      payIdx: e.currentTarget.dataset.index
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let page = this;
    this.setData({isIos: app.globalData.isIos});
    page.fetchCompany();
    core.get({url: '/xilujob.common/corporate',loading:false,success(ret, data) {
        page.setData({
          config: ret.data
        });
      }
    });
    page.fetchPackageList();
    page.fetchPackageOrderList();
  },

  fetchCompany(){
    let page = this;
    core.get({url: '/xilujob.company/company',loading:false,success(ret, data) {
      page.setData({
        summary: ret.data.summary
      });
    }});
  },
  fetchPackageList() {
    util.fetch(this, 'xilujob.package/lists', {}, 'packageListMore', 'packageList', 'data', list=>{

    });
  },

  fetchPackageOrderList() {
    util.fetch(this, 'xilujob.package_order/lists', {}, 'packageOrderListMore', 'packageOrderList', 'data', list=>{

    });
  },

  choosePackage(e){
    let packageIndex = e.currentTarget.dataset.index;
    this.setData({
      packageIndex:packageIndex,
      showPop: !this.data.showPop
    })
  },
  //发票
  ticketInput(e){
    this.setData({
      ticket: e.detail.value.trim()
    })
  },
  //备注
  remarksInput(e){
    this.setData({
      remarks: e.detail.value.trim()
    })
  },

  /**
   * 套餐购买
   */
  onsubmit(){
    let packageIndex = this.data.packageIndex;
    let packageList = this.data.packageList;
    let package_id = packageList[packageIndex].id;
    let config = this.data.config;
    let ticket_type = this.data.ticketType;
    let ticket = this.data.ticket;
    let pay_type = this.data.payIdx;
    let remarks = this.data.remarks;
    let data = {
      package_id:package_id,
      ticket_type:ticket_type,
      ticket:ticket,
      pay_type:pay_type,
      corporate_company:config.corporate_company,
      open_bank:config.open_bank,
      corporate_account:config.corporate_account,
      remarks:remarks
    }
    let page = this;
    if(pay_type == 1){
      wx.login({
        success(wxRes) {
          let code = wxRes.code;
          core.post({
            url: '/xilujob.user/wxlogin',
            data: {code: code},
            success: (ret, response) => {
              data['third_id'] = ret.data.third.third_id;
              page.createOrder(data);
            },fail: (ret, response) => {
              wx.showToast({
                title: '下单失败',
                icon:'none'
              })
            }
          });
        },
      });
    }else{
      page.createOrder(data);
    }
  },

  createOrder(data){
    let page = this;
    core.post({url: '/xilujob.package/order',data:data,loading:true,success(ret, data2) {
      if(ret.data.pay_type == 3){
        wx.showModal({
          title: '提示',
          content:"订单已创建成功,请尽快线下打款~",
          success(){
            setTimeout(function(){
              wx.redirectTo({
                url: '/pages/order_detail/order_detail?order_id='+ ret.data.id,
              })
            },1500)
          }
        })
      }else{
        page.payment(ret.data,data.third_id)
      }
     
    }});
  },

  payment(order,third_id){
    let page = this;
    core.post({url: '/xilujob.pay/pay',data:{order_id:order.id,platform:order.platform,pay_type:order.pay_type,third_id:third_id},loading:true,success(ret, data) {
      if(order.pay_type == 1){
          //发起支付
          wx.requestPayment({
            'timeStamp': ret.data.timeStamp,
            'nonceStr': ret.data.nonceStr,
            'package': ret.data.package,
            'signType': ret.data.signType,
            'paySign': ret.data.paySign,
            'success': function (res) {
              wx.showToast({
                icon: "none",
                title: '支付成功',
              });
              page.togglePop();
              page.fetchCompany();
            },
            'fail': function (res) {
              wx.showToast({
                icon: "none",
                title: '支付失败',
              })
            },
            'complete': function (res) {
              
            }
          });
      }else{
        wx.showToast({
          title: ret.msg,
          icon:'none'
        })
      }
    }});
  },

  //复制
  copy(){
    let config = this.data.config;
    let data = config.corporate_company+' '+config.open_bank+' '+ config.corporate_account
    wx.setClipboardData({
      data: data,
      success (res) {
        
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  
})
